package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhouchao.bean.RepairBean;
import com.zhouchao.dao.RepairDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.TimeUtil;

public class Repair extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	RepairDao rd = new RepairDao();
	Connection con = null;
	DbUtil dbUtil = new DbUtil();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		if ("set".equals(action)) {
			
			String name1 = request.getParameter("name");
			String name=new String(name1.getBytes("ISO-8859-1"),"utf-8");
			int buildid = Integer.parseInt(request.getParameter("id"));

			String room = request.getParameter("room");
			request.setAttribute("name", name);
			
			request.setAttribute("id", buildid);
			request.setAttribute("room", room);
			request.setAttribute("time", tm);
			request.setAttribute("mainPage", "student/repairadd.jsp");
			request.getRequestDispatcher("mainStudent.jsp").forward(request,response);
		} else if ("stulist".equals(action)) {
			String name1 = request.getParameter("name");
			String name=new String(name1.getBytes("ISO-8859-1"),"utf-8");
			System.out.println("name=="+name);
			String buildid = request.getParameter("id");
			String room = request.getParameter("room");
			List<RepairBean> repairList = new ArrayList<RepairBean>();

			try {
				con = dbUtil.getCon();
				repairList = rd.repairList(con, buildid, room);

				request.setAttribute("name", name);
				request.setAttribute("id", buildid);
				request.setAttribute("room", room);
				request.setAttribute("repairList", repairList);
				request.setAttribute("mainPage", "student/repair.jsp");
				request.getRequestDispatcher("mainStudent.jsp").forward(
						request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else if ("list".equals(action)) {
			List<RepairBean> repairList = new ArrayList<RepairBean>();
			try {
				con = dbUtil.getCon();
				repairList = rd.repairList(con);
				request.setAttribute("repairList", repairList);

				request.setAttribute("mainPage", "dormManager/repair.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("end".equals(action)) {
			String id = request.getParameter("id");
			List<RepairBean> repairList = new ArrayList<RepairBean>();
			try {
				con = dbUtil.getCon();
				rd.repairUpdate(con, id);
				repairList = rd.repairList(con);
				request.setAttribute("repairList", repairList);

				request.setAttribute("mainPage", "dormManager/repair.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(
						request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name");
			String room = request.getParameter("room");
			String date = request.getParameter("date");
			String tel = request.getParameter("tel");
			String tt = request.getParameter("tt");
			
			
			RepairBean re = new RepairBean(id, room, date, name, tel, tt, 0);

			try {
				con = dbUtil.getCon();
				rd.repairAdd(con, re);
				request.setAttribute("name", name);
				request.setAttribute("id", id);
				request.setAttribute("room", room);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "student/repairadd.jsp");
				request.getRequestDispatcher("mainStudent.jsp").forward(
						request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
